package com.kz.himalaya.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kz.himalaya.R;
import com.kz.himalaya.adapter.TabAdapter;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.HomeTabEntity;
import com.kz.himalaya.bean.TabsData;
import com.kz.himalaya.databinding.FragmentHomePageBinding;
import com.kz.himalaya.fragment.homepage.AnchorFragment;
import com.kz.himalaya.fragment.homepage.ClassifyFragment;
import com.kz.himalaya.fragment.homepage.HotFragment;
import com.kz.himalaya.fragment.homepage.RecommendFragment;
import com.kz.himalaya.fragment.homepage.TopFragment;
import com.kz.himalaya.interfaces.HttpService;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomePageFragment extends Fragment implements Callback<HomeTabEntity> {


    private FragmentHomePageBinding binding;
    private TabAdapter adapter;
    private ArrayList<Fragment> fragments;

    public HomePageFragment() {
        // Required empty public constructor
    }

    public static HomePageFragment newInstance() {

        Bundle args = new Bundle();

        HomePageFragment fragment = new HomePageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_page,container,false);
        HttpService service = ((MyApplication) getActivity().getApplication()).getService();
        initFragment();
        adapter = new TabAdapter(getActivity().getSupportFragmentManager(),new ArrayList<TabsData.Tab>(),fragments);
        binding.pagerHome.setAdapter(adapter);
        binding.tabHome.setupWithViewPager(binding.pagerHome);

        service.getHomeTab().enqueue(this);
        return binding.getRoot();
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(RecommendFragment.newInstance());
        fragments.add(HotFragment.newInstance());
        fragments.add(ClassifyFragment.newInstance());
        fragments.add(TopFragment.newInstance());
        fragments.add(AnchorFragment.newInstance());
    }

    @Override
    public void onResponse(Call<HomeTabEntity> call, Response<HomeTabEntity> response) {
        HomeTabEntity body = response.body();
        if (body != null) {
            if (body.getMsg().equals(getContext().getString(R.string.request_success_flag))) {
                adapter.addAll(body.getTabs().getList());
                binding.pagerHome.setCurrentItem(1);
            }
        }
    }

    @Override
    public void onFailure(Call<HomeTabEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
